package com.kai.awt.layoutmanager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Author Mr.Yan
 * @create 2022/3/20 13:51
 * @description: CardLayout
 */
public class Demo6 {

    public static void main(String[] args) {



        Frame frame = new Frame("cardLayOut");

        CardLayout layout = new CardLayout();
        Panel panel = new Panel(layout);
        for (int i = 1; i <= 5; i++) {
            panel.add("第"+i+"张",new JButton("第"+i+"张"));
        }

        frame.add(panel);

        ActionListener listener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String actionCommand = e.getActionCommand();
                switch (actionCommand) {
                    case "第一张":
                        layout.first(panel);
                        break;
                    case "最后一张":
                        layout.last(panel);
                        break;
                    case "上一张":
                        layout.previous(panel);
                        break;
                    case "下一张":
                        layout.next(panel);
                        break;
                    case "第三张":
                        layout.show(panel,"第三张");
                        break;
                    default:
                        break;
                }
            }
        };

        Panel panel1 = new Panel();
        JButton b1 = new JButton("上一张");
        JButton b2 = new JButton("下一张");
        JButton b3 = new JButton("第一张");
        JButton b4 = new JButton("最后一张");
        JButton b5 = new JButton("第三张");
        b1.addActionListener(listener);
        b2.addActionListener(listener);
        b3.addActionListener(listener);
        b4.addActionListener(listener);
        b5.addActionListener(listener);

        panel1.add(b1);
        panel1.add(b2);
        panel1.add(b3);
        panel1.add(b4);
        panel1.add(b5);

        frame.add(panel1,BorderLayout.NORTH);
        frame.pack();
        frame.setVisible(true);
    }
}
